package com.tofine.smarthome.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tofine.smarthome.R;
import com.tofine.smarthome.model.Device;
import com.tofine.smarthome.ui.DeviceInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author ddMax
 * @since 2016/4/29 0:59.
 */
public class DeviceChooseAdapter extends RecyclerView.Adapter<DeviceChooseAdapter.ViewHolder>{

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.device_item) RelativeLayout mItem;
        @Bind(R.id.device_image) ImageView mDeviceImage;
        @Bind(R.id.device_title) TextView mDeviceTitle;
        @Bind(R.id.device_checkbox) CheckBox mDeviceCheckBox;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private Context mContext;
    private List<Device> mDataSet;
    private ArrayList<Device> mSelectedDevices;

    public DeviceChooseAdapter(Context context, List<Device> dataSet) {
        this.mContext = context;
        this.mDataSet = dataSet;
        this.mSelectedDevices = new ArrayList<>();
    }

    /**
     * 创建ViewHolder
     * @param parent 对应的父视图
     * @param viewType 一个标志，我们根据该标志可以实现渲染不同类型的ViewHolder
     * @return ViewHolder对象
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_device_choose, parent, false);
        return new ViewHolder(view);
    }

    /**
     * 绑定每个ViewHolder的数据
     * @param holder 当前的ViewHolder实例
     * @param position ViewHolder实例的位置
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Device device = mDataSet.get(position);
        // 整个Item点击监听
        holder.mItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, DeviceInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("device", device);
                bundle.putInt("mode", DeviceInfoActivity.MODE_EDIT);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        // 设备图片
        Picasso.with(mContext).load(Uri.parse(device.image)).into(holder.mDeviceImage);
        // 设备名称
        holder.mDeviceTitle.setText(device.name);
        // CheckBox
        holder.mDeviceCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mSelectedDevices.add(mDataSet.get(position));
                } else {
                    mSelectedDevices.remove(mDataSet.get(position));
                }
            }
        });
    }

    /**
     * Adapter中Item总数
     * @return Item总数
     */
    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    public ArrayList<Device> getSelectedDevices() {
        return this.mSelectedDevices;
    }

}
